using System;
using System.Collections.Generic;
using System.Text;

namespace MessageLibrary
{
    [Serializable]
    public class LogOutMessage : AbsMessage
    {
        /// <summary>
        /// A message that informs both client and server 
        /// that a client has logged out
        /// </summary>
        /// <param name="name">client's name</param>
        public LogOutMessage(string name):base (name)
        {

        }


        public override void HandleMessage(System.Windows.Forms.RichTextBox textBox, Client c)
        {
            ///if a client object was entered,
            ///it's listening thread is stopped.
            ///this occurs only on the server side.
            if (c!=null)
            {
                c.Stop();
            }

            ///updates the client and server through the
            ///given textbox.
            if (textBox != null)
            {
                if (textBox.InvokeRequired)
                {
                    MessageInvoker mi = new MessageInvoker(HandleMessage);
                    textBox.Invoke(mi, textBox, c);
                }
                else
                {
                    //finds the end of the chat conversation
                    textBox.SelectionStart = textBox.Text.Length;
                    //prints the time of the message
                    textBox.SelectedText = time.ToShortTimeString();
                    //print the sender and the message
                    textBox.SelectedText = " " + username + " has logged out. \n";
                    //scrolls the text box down
                    textBox.ScrollToCaret();

                }
            }

        }
    }
}
